package com.ai.web.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ai.web.cache.Constants;
import com.ai.web.cache.UserContext;
import com.ai.web.service.CommentRepairService;
import com.ai.web.service.RepairService;
import com.ai.web.vo.CommentRepairV;
import com.ai.web.vo.QueryData;
import com.ai.web.vo.RepairV;
import com.ai.web.vo.ResultModel;

/**
 * 设备报修评价
* @ClassName: CommentRepairController 
* @Description: TODO 
* @author:  
* @date: 2014年3月24日
*
 */
@Controller
@RequestMapping("/commentRepair")
public class CommentRepairController extends BaseController {
    

    private static Logger logger = Logger.getLogger(CommentRepairController.class);
    
    @Autowired
    private CommentRepairService commentRepairService;
    @Autowired
    private RepairService repairService;
    
    @RequestMapping(value = "/list")
    public String list() {
        
        logger.info("hello......test");
        return "commentRepair/commentRepair_list";
    }
    
    @RequestMapping(value = "/query", produces = "application/json")
    @ResponseBody
    public QueryData query(@RequestParam HashMap<String, Object> paramMap)
            throws Exception {
        
        Map<String, Object> conditionMap = buildCondition(paramMap);
        logger.info("---- " + conditionMap);
        
        QueryData qd = commentRepairService.queryListAndCount(conditionMap);
        
        return qd;
    }
    
    /**
     * 导出
     * @param paramMap
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/exportExcel")
    public void exportExcel(@RequestParam HashMap<String, Object> paramMap)
            throws Exception {
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    	if(paramMap==null){
    		paramMap = new HashMap<String, Object>();
    	}
        logger.info("---- " + paramMap);
        //String[] keys = {"评价人","手机号码","评价人所属单位","体验满意度","客服满意度","评语","评价时间"};
        String[] keys = {"commenterName","mobilePhone","commentCompany","useSat","customerServiceSat","comment","createTime"};
        List<CommentRepairV> dataList = (List<CommentRepairV>)commentRepairService.queryList(paramMap);;
        List<Map<String, String>> dataSet = new ArrayList<Map<String,String>>();
        Map<String, String> map = new HashMap<String, String>();
    	map.put("commenterName", "评价人");
    	map.put("mobilePhone", "手机号码");
    	map.put("commentCompany", "评价人所属单位");
    	map.put("useSat", "体验满意度");
    	map.put("customerServiceSat", "客服满意度");
    	map.put("comment", "评语");
    	map.put("createTime", "评价时间");
    	dataSet.add(map);
        for(CommentRepairV v : dataList){
        	map = new HashMap<String, String>();
        	map.put("commenterName", v.getCommenterName());
        	map.put("mobilePhone", v.getMobilePhone());
        	map.put("commentCompany", v.getCommentCompany());
        	map.put("useSat", v.getUseSat()+"");
        	map.put("customerServiceSat", v.getCustomerServiceSat()+"");
        	map.put("comment", v.getComment());
        	map.put("createTime", sdf.format(v.getCreateTime()));
        	dataSet.add(map);
        }
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmm");
        exportXls(dataSet, keys, "commentRepair"+df.format(new Date())+".xls");
    }
    
    @RequestMapping(value = "/new")
    public String newCommentRepair(Model model) {
        
        CommentRepairV commentRepairV = new CommentRepairV();
        model.addAttribute(commentRepairV);
        
        return "commentRepair/commentRepair_template";
    }
    
    
    @RequestMapping(value = "/update/{commentRepairId}")
    public String updateCommentRepair(@PathVariable("commentRepairId") int commentRepairId, Model model)
            throws Exception {
        
        CommentRepairV commentRepairV = commentRepairService.getCommentRepair(commentRepairId);
        logger.info("----- getUser:" + commentRepairV);
        
        if (commentRepairV == null) {
            // exception redirect error page
            // TODO
        }
        model.addAttribute(commentRepairV);
        
        return "commentRepair/commentRepair_template";
    }
    
    @RequestMapping(value = "/save", produces = "application/json")
    @ResponseBody
    public ResultModel saveCommentRepair(CommentRepairV commentRepairV) throws Exception {
    	commentRepairV.setCreateTime(new Date());
    	RepairV repair = repairService.getRepair(commentRepairV.getRepairId());
    	commentRepairV.setCommentCompany(repair.getBankUserCompany());
        ResultModel rm = commentRepairService.saveCommentRepair(commentRepairV);
        return rm;
    }
    
    @RequestMapping(value = "/delete", produces = "application/json")
    @ResponseBody
    public ResultModel deleteCommentRepair(@RequestParam String delIds)
            throws Exception {
        
        System.out.println(delIds);
        
        List<Integer> userIds = getDeleteIds(delIds);
        ResultModel rm = commentRepairService.deleteCommentRepairs(userIds);
        
        return rm;
    }
    
    
}
